package com.hdoit.kylin.web.studentManager.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.studentManager.model.Data;
import com.hdoit.kylin.logical.studentManager.service.IDataService;
import com.hdoit.kylin.logical.studentManager.service.IExcel_headService;
import com.hdoit.kylin.logical.viewmodel.DataSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;

@Controller
@RequestMapping(value = "/admin/experimentExcel/data/")
public class DataController extends BaseController{
	@Override
	public String getRoutePath() {
		return "areas/admin/experimentExcel/data/";
	}
	private IExcel_headService excel_headService;
	private IDataService dataService;
	
	public IDataService getDataService() {
		return dataService;
	}
	@Resource
	public void setDataService(IDataService dataService) {
		this.dataService = dataService;
	}

	public IExcel_headService getExcel_headService() {
		return excel_headService;
	}
	
	@Resource
	public void setExcel_headService(IExcel_headService excel_headService) {
		this.excel_headService = excel_headService;
	}

	//private String key1 = "";
	//Excel_headSearch key =new Excel_headSearch();
	private static String mark="";
	private final String indexViewName = "index";
	private final String index2ViewName = "index2";
	private final String output2ViewName = "output2";
	//private final String output3ViewName = "output3";
	private final String detailedViewName = "dataDetailed";
	private final String tabsViewName = "tabs";
	private final String saveViewName = "save";
	private final String saveValueViewName = "saveValue";
//	private final String dictColumnJsonViewName = "dictColumnJson";
	
	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,DataSearch search,boolean ajaxPost,boolean label,String excelId){
		List<Data> dataTrees=null;
		search.setExcelId(excelId);
		dataTrees = dataService.queryListByExcelId(search);
		int num = dataTrees.size();
		if(label==true){
			Data data = new Data();
			data.setSortCode(num*5);
			data.setVersion("new tab");
			data.setId("new");
			dataTrees.add(data);
			model.addAttribute("search", search);
	    	model.addAttribute("dataTrees",dataTrees);
			return view(tabsViewName);
		}else{
			if(num == 0){
				Data data = new Data();
				data.setSortCode(num*5);
				data.setVersion("new tab");
				data.setId("new");
				dataTrees.add(data);
			}
		}
    	
    	model.addAttribute("search", search);
    	model.addAttribute("dataTrees",dataTrees);
        return view(indexViewName);
    }
	
	@RequestMapping(value = index2ViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index2(Model model,DataSearch search,PagerModel pagerModel, boolean ajaxPost,boolean label,String key){
		search.setExcelId(key);
		System.out.println(key+"22222");
		pagerModel.setTotalAmount(dataService.queryCount(search));
		List<Data> datas = dataService.queryList(search, pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("dataSearch", search);
		model.addAttribute("datas", datas);
		if(ajaxPost){
			return view(output2ViewName);
		}
		return view(index2ViewName);
	}
	

	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET })
	public String finalSpeechDetailed(Model model, String id,String excelId) {
		Data data = new Data();
		if (!StringUtils.isEmpty(id)) {
			data = dataService.queryModelById(id);
		}
		model.addAttribute("data", data);
		return view(detailedViewName);
	}
	
//	@ResponseBody
//    @RequestMapping(value = dictColumnJsonViewName, method = { RequestMethod.POST},produces = "text/json;charset=UTF-8")
//    public String dictColumnJson(String id){
//		System.out.println("11111");
//		System.out.println(id+"2222222222");
//		String datas;
//		if(dataService.queryCountById(id)!=0){
//			datas = dataService.queryModelById(id).getMarkValue();
//		}else{
//			datas = null;
//		}	
//    	System.out.println(datas+"2222");
//    	return JsonUtils.writeValueAsString(datas);
//    }
	 
	    @RequestMapping(value = saveViewName, method = { RequestMethod.POST},produces = "text/json;charset=UTF-8")
	    public String save(Model model,Data data,String id,String excelId){
	    	if(dataService.queryCountById(id)<=0){
	    		System.out.println((dataService.queryCountById(id)<=0)+"99999");
	    		System.out.println("000000");	
	    		System.out.println(excelId+"75616");
		    	data.setExcelId(excelId);
		    	System.out.println(data.getVersion()+"66666");
		    	System.out.println(id+"1111");
	
		    	data.setEmployeeId(getLoginModel().getLoginId());
		    	mark = CommonUtils.getKeyId();
		    	System.out.println(mark+"111");
		   		data.setId(mark);
		   		DataSearch search = new DataSearch();
		   		search.setExcelId(excelId);
		   		data.setSortCode(dataService.queryCount(search)*5);
		   		data.setModelStatus(ModelStatus.add);
	    	}else{
	    		System.out.println("8888");
	    		
	    		System.out.println(excelId+"444");
	    		data.setModelStatus(ModelStatus.update);
	    	}   	
	    	ActionResult actionResult;
			if (dataService.applyChange(data)) {
				actionResult = new ActionResult();
			} else {
				actionResult = new ErrorResult();
			}
			model.addAttribute("data", data);
			model.addAttribute(ManageContainer.RESULTKEY, actionResult);
			return view(detailedViewName);
	    }
	    
	    @ResponseBody
	    @RequestMapping(value = saveValueViewName, method = { RequestMethod.POST})
	    public ActionResult saveValue(@RequestBody String datas,String id){
	    	System.out.println(id);
	    	Data data = new Data();
	    	if(StringUtils.isEmpty(id))
	    		id=mark;
	    	System.out.println(mark+"454545");
	    	data = dataService.queryModelById(id);
	    	if (dataService.queryCountById(id)!=0) {
	    		System.out.println(datas+"12552");
	    		data.setMarkValue(datas);
	    		data.setModelStatus(ModelStatus.update);
	    	}	
	    	ActionResult result;
	    	if(dataService.applyChange(data)){
	    		result = new ActionResult();
	    		return result;
	    	}else{
	    		result = new ErrorResult();
	    		return result;
	    	}
	    }
}
